﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.UI.Web.MVP.View;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.Web.MVP.Presentation
{
    public abstract class NodeTypeEditModulePresenter<TView>: ModulePresenter<TView>
        where TView : class, INodeTypeEditModuleView
    {
        public virtual string ReturnUrl
        {
            get { return View.QueryString.GetValue<string>(DefaultParamNames.ReturnURL) != null
                            ? View.QueryString.GetValue<string>(DefaultParamNames.ReturnURL).DecodeBase64()
                            : null; }
        }

        public bool IsEdition
        {
            get { return View.TypeId != null; }
        }

        public virtual Guid? ParentTypeId { get { return View.QueryString.GetValue<Guid?>(DefaultParamNames.ParentNodeID); } }

        protected NodeTypeEditModulePresenter(TView view)
            : base(view)
        {
        }

        protected override void Initialize(object sender, EventArgs e)
        {
            base.Initialize(sender, e);

            View.OnSave += OnNodeTypeSave;

            if(!View.IsPostBack)
            {
                if (View.CancelButton != null)
                {
                    if(ReturnUrl != null)
                        View.CancelButton.NavigateUrl = ReturnUrl;
                }

                if(IsEdition)
                {
                    SetValues();
                }
            }
        }

        protected virtual void SetValues()
        {
            var currentType = NodeTypeManager.GetNodeType(View.TypeId.Value);

            View.NodeTypeNameValuesByCulture = Translator.GetResources("TypeManagement",
                    currentType.ID + "_TypeName",
                    ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));;
            View.IconPath = currentType.IconPath;
            View.CanHaveChildren = currentType.IsInheritable;
            View.IsGenerated = currentType.IsGenerated;
            View.IsAbstract = currentType.IsAbstract;
            View.IsListable = currentType.Listable;
            View.IsVersioned = currentType.IsVersioned;
        }

        protected virtual NodeType NewNodeType()
        {
            var type = new NodeType(GetNodeTypeName(),
                                    View.CanHaveChildren,
                                    View.IsGenerated,
                                    View.IsAbstract,
                                    false,
                                    ParentTypeId,
                                    false,
                                    new List<NodeType>(),
                                    new List<Node>(),
                                    new List<TypeTemplate>(),
                                    null)
            {
                Indexable = true,
                Listable = View.IsListable,
                IconPath = View.IconPath,
                IsVersioned = View.IsVersioned,
                IsAdmin = View.IsAdmin
            };

            return type;
        }

        protected void OnNodeTypeSave(object sender, EventArgs e)
        {
            if (IsEdition)
            {
                UpdateNodeType();
            }
            else
            {
                SaveNodeType();
            }
            View.Redirect(ReturnUrl);
        }

        protected virtual void UpdateNodeType()
        {
            var type = NodeTypeManager.GetNodeType(View.TypeId.Value);

            type.IconPath = View.IconPath;

            NodeTypeManager.SaveType(type);

            Translator.CreateResource("TypeManagement", View.TypeId + "_TypeName", View.NodeTypeNameValuesByCulture);
        }

        protected virtual void SaveNodeType()
        {
            var type = NewNodeType();

            NodeTypeManager.SaveType(type);

            Translator.CreateResource("TypeManagement", type.ID + "_TypeName", View.NodeTypeNameValuesByCulture);
        }

        protected virtual string GetNodeTypeName()
        {
            var name = View.NodeTypeNameValuesByCulture.ContainsKey("en-CA")
                ? View.NodeTypeNameValuesByCulture["en-CA"]
                : View.NodeTypeNameValuesByCulture.First().Value;

            return name.ToCodeSafe();
        }
    }
}